<template>
    <div>
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" @close="cancel">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="客户名称" prop="customerId">
                <el-select v-model="form.customerId" placeholder="请选择客户信息" style="width:100%" filterable clearable :disabled="!canChooseCustomer" @change="changeCustomer">
                    <el-option
                        v-for="dict in customerList"
                        :key="dict.customerId"
                        :label="dict.customerName"
                        :value="dict.customerId"
                        />
                </el-select>
            </el-form-item>
            <el-form-item label="付款方式">
                <el-select v-model="paymentType" placeholder="请选择付款方式" style="width:100%" disabled >
                    <el-option
                        v-for="dict in paymentTypeOptions"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="dict.dictValue"
                        />
                </el-select>
            </el-form-item>
            <el-form-item label="合同编号" prop="contractNo">
                <el-input v-model="form.contractNo" placeholder="请输入合同编号" />
            </el-form-item>
            <el-form-item label="项目信息" prop="projectName">
                <el-input v-model="form.projectName" placeholder="请输入项目信息" />
            </el-form-item>
            <el-form-item label="负责人" prop="director">
                <el-input v-model="form.director" placeholder="请输入负责人" />
            </el-form-item>
            <el-form-item label="合同金额" prop="contractSum">
                <el-input-number v-model="form.contractSum" placeholder="请输入合同金额" :max="999999999.99" style="width:100%"></el-input-number>
                <!-- <el-input v-model="form.contractSum" placeholder="请输入合同金额" /> -->
            </el-form-item>
            <el-form-item label="交货日期" prop="deliveryDate" >
                <el-date-picker clearable size="small"
                    v-model="form.deliveryDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择交货日期" style="width:100%">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="开启提醒" prop="openRemind">
                <el-switch
                    v-model="form.openRemind"
                    active-value="1"
                    inactive-value="0">
                </el-switch>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>  
    </div>
</template>
<script>
import { getContract, addContract, updateContract } from "@/api/business/contract";
import { listCustomer, getCustomer} from "@/api/business/customer";

export default {
  name: "ContractEdit",
  props: {
    show: { type: Boolean, required: true },
    editConfig: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      title:'',
      open: false,
      // 是否能选择客户
      canChooseCustomer: true,
      // 客户列表
      customerList: [],
      paymentType:'',
      // 付款方式数据字典
      paymentTypeOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户id不能为空", trigger: "blur" }
        ],
        contractNo: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        contractSum: [
          { required: true, message: "合同金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("bus_payment_type").then(response => {
      this.paymentTypeOptions = response.data;
    });
  },
  mounted(){
  },
  methods: {
    // 查询客户信息列表
    getCustomerList() {
      let queryParams= {
        pageNum: 1,
        pageSize: 99,
        customerName: null,
        paymentType: null,
      }

      listCustomer(queryParams).then(response => {
        this.customerList = response.rows;
      });
    },
    
    getCustomerPaymentType(customerId){
      if(customerId == null || customerId == ''){
        this.paymentType = ''
        return
      }
      getCustomer(customerId).then(response => {
          this.paymentType = response.data.paymentType;
      });
    },
    // 更改客户信息列表
    changeCustomer(val) {
      this.getCustomerPaymentType(val)
    },
    // 取消按钮
    cancel() {
      this.$emit('update:show', false)
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        customerId: null,
        contractNo: null,
        projectName: null,
        director: null,
        contractSum: null,
        deliveryDate: null,
        openRemind: null,
        completeType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
      this.paymentType = ''
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            updateContract(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.cancel();
            });
          } else {
            addContract(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.cancel();
            });
          }
        }
      });
    }
  },
  watch: {
    show: {
        immediate: true,
        handler: function(val) {
            this.open = val
            if (val) {
              this.getCustomerList()
              this.reset()
              this.title = "添加合同"
              // 由客户管理页面进入的添加合同页面
              if(this.editConfig.customerId && this.editConfig.customerId !=''){
                  this.canChooseCustomer = false
                  this.form.customerId = this.editConfig.customerId
                  this.paymentType = this.editConfig.paymentType
              }
              // 由合同管理页面进入的维护合同页面
              if(this.editConfig.id && this.editConfig.id !=''){
                  this.title = "修改合同"
                  this.id = this.editConfig.id
                  this.canChooseCustomer = true
                  getContract(this.id).then(response => {
                      this.form = response.data;
                      this.getCustomerPaymentType(this.form.customerId)
                  });
              }
            }
        }
    }
  }
};
</script>
